// CherryMerry ( Mikhail Tomilenko ), Reverse Entertainment, 2021-2024

#include "MainMenu.h"

#include <NifestGame/Core/Public/NF_GameInstance.h>

#include "Components/Button.h"
#include "Components/TextBlock.h"
#include "Kismet/GameplayStatics.h"
#include "Kismet/KismetSystemLibrary.h"


void UMainMenu::NativeConstruct()
{
	if (TextBlock_GameName)
	{
		TextBlock_GameName->SetText(FText::FromString(UKismetSystemLibrary::GetGameName()));
	}
	if (Button_StartGame)
	{
		Button_StartGame->OnClicked.AddDynamic(this, &UMainMenu::OnStartGameButtonClicked);
		UE_LOG(LogTemp, Warning, TEXT("MainMenu: Button_StartGame initialized"))
	}
	if (Button_LoadGame)
	{
		Button_LoadGame->OnClicked.AddDynamic(this, &UMainMenu::OnLoadGameButtonClicked);
		UE_LOG(LogTemp, Warning, TEXT("MainMenu: Button_LoadGame initialized"))
	}
	if (Button_Settings)
	{
		Button_Settings->OnClicked.AddDynamic(this, &UMainMenu::OnSettingsButtonClicked);
		UE_LOG(LogTemp, Warning, TEXT("MainMenu: Button_Settings initialized"))
	}
	if (Button_Quit)
	{
		Button_Quit->OnClicked.AddDynamic(this, &UMainMenu::OnQuitButtonClicked);
		UE_LOG(LogTemp, Warning, TEXT("MainMenu: Button_Quit initialized"))
	}
}

void UMainMenu::OnStartGameButtonClicked()
{
	if (UNF_GameInstance* GameInstance = Cast<UNF_GameInstance>(UGameplayStatics::GetGameInstance(GetWorld())))
	{
		GameInstance->StartGame(EStartGameType::SGTE_NewGame);
	}
}

void UMainMenu::OnLoadGameButtonClicked()
{
	if (UNF_GameInstance* GameInstance = Cast<UNF_GameInstance>(UGameplayStatics::GetGameInstance(GetWorld())))
	{
		GameInstance->StartGame(EStartGameType::SGTE_LoadGame);
	}
}

void UMainMenu::OnSettingsButtonClicked()
{
}

void UMainMenu::OnQuitButtonClicked()
{
	UKismetSystemLibrary::QuitGame(this, UGameplayStatics::GetPlayerController(GetWorld(), 0), EQuitPreference::Quit, true);
}
